
var Nowpage=1;
function init(){
	
	firstLoad(1,0);
	document.getElementById("Edit").addEventListener("click",edit,false);
	document.getElementById("showall").addEventListener("click",list,false);
	document.getElementById("search").addEventListener("click",search,false);
	document.getElementById("checkAll").addEventListener("click",()=>{
		checke();
	},false);
	document.getElementById("jump").addEventListener("click",()=>{
	let jumppage=document.getElementById("getPageNumber").value;firstLoad(jumppage,0)},false);
	document.getElementById("pageFirst").addEventListener("click",()=>{
		firstLoad(1,0)
		},false);
	document.getElementById("pageEnd").addEventListener("click",()=>{
		
		firstLoad(null,1);
		},false);
	document.getElementById("pageNext").addEventListener("click",pageNext,false);
	document.getElementById("pageLast").addEventListener("click",()=>{
		Nowpage--;
		firstLoad(Nowpage,0);
		},false);
	document.getElementById("pageLast").style.visibility="hidden";
}


function pageNext(){
	
		Nowpage++;
		firstLoad(Nowpage,0);
		
}
function edit(){
	document.getElementById("message").style.display="none";
	document.getElementById("editmessage").style.visibility="visible";
	
	let id=0;
	var allcheck=document.getElementsByClassName("list");
	var count=0;
	for(var i=0;i<allcheck.length;i++){
		  var obj = allcheck[i];
		 if(obj.checked==true){
		  id=obj.value;
		 count++;}
		  }
	if(count>1){
		alert("多行选择，仅修改最下面一个数据");
	}
	let au=new AjaxUtils;
	let param="usermessageadmin.servlet?param=EditUser&editid="+id;
	
	au.ajaxRequest(param,(xhr)=>{

		let json=JSON.parse(xhr.responseText);
		alert("将要修改"+json.date.userName);
		document.getElementById("userID").value=json.date.id;
		document.getElementById("userName").value=json.date.userName;
		document.getElementById("password").value=json.date.password;
		document.getElementById("email").value=json.date.email;
		 let hobbylist=json.date.hobby;
		 for(let i=0;i<hobbylist.length;i++){
			 document.getElementById(hobbylist[i]).checked=true;
		 }
		 if(json.date.sex==1){
			 document.getElementById("woman").checked=true;
		 }
		 else{
			 document.getElementById("man").checked=true;
		 }
		 document.getElementById("province").value=json.date.place.substring(0,2);
		 value=document.getElementById("province").value;
		 let param="login.servlet?param=getcity&";
			param+="province="+value;
			 let au=new AjaxUtils;
	        au.ajaxRequest(param,(xhr)=>{
	        		document.getElementById("city").innerHTML=xhr.responseText;
	        	});
	       
		 
		 document.getElementById("city").value=json.date.place;
		 
	});
}
function search(){
	reloading();
}
function list(){
	firstLoad(1,0);
	document.getElementById("pageFirst").style.visibility="visible";
	document.getElementById("pageLast").style.visibility="visible";
	document.getElementById("pageNext").style.visibility="visible";
	document.getElementById("pageNumberInsert").style.visibility="visible";
	document.getElementById("pageEnd").style.visibility="visible";
	document.getElementById("getPageNumber").style.visibility="visible";
	document.getElementById("jump").style.visibility="visible";
	document.getElementById("showall").style.visibility="hidden";
	
}
function reloading(){
	document.getElementById("showall").style.visibility="visible";
	document.getElementById("pageFirst").style.visibility="hidden";
	document.getElementById("pageLast").style.visibility="hidden";
	document.getElementById("pageNext").style.visibility="hidden";
	document.getElementById("pageNumberInsert").style.visibility="hidden";
	document.getElementById("pageEnd").style.visibility="hidden";
	document.getElementById("getPageNumber").style.visibility="hidden";
	document.getElementById("jump").style.visibility="hidden";
	let au=new AjaxUtils;
	let param="usermessageadmin.servlet?param=get";
	
	au.ajaxRequest(param,(xhr)=>{
		
		let json=JSON.parse(xhr.responseText);
		let insertPoint=document.getElementById("usermessageInsertPoint");
		insertPoint.innerHTML="<tr style='background:red'><td><input type='checkbox'  id='checkAll' ></td><td>用户名</td><td>密码</td><td>性别</td><td>爱好</td><td>来自于</td><td>邮箱</td></tr>";
		for(let i=0;i<json.userMessage.length;i++){
				if(testsear(json.userMessage[i])){
							let tr=document.createElement("tr");
							let td1=document.createElement("td");
										let input1=document.createElement("input");
										input1.type="checkbox";
										input1.className="list";
										input1.value=json.userMessage[i].id;
										td1.appendChild(input1);
							let td2=document.createElement("td");
								td2.innerHTML=json.userMessage[i].userName;
							let td3=document.createElement("td");
								td3.innerHTML=json.userMessage[i].password;
							let td4=document.createElement("td");
								if(json.userMessage[i].sex==1){
								td4.innerHTML="女";
								}else{
								td4.innerHTML="男";
								}
							let td5=document.createElement("td");
								for(let m=0;m<json.userMessage[i].hobbyName.length;m++){
									if(m==0){
										td5.innerHTML=json.userMessage[i].hobbyName[m];
									}
									else{td5.innerHTML=td5.innerHTML+","+json.userMessage[i].hobbyName[m];
									}
								}
							let td6=document.createElement("td");
							td6.innerHTML=json.userMessage[i].provinceName+","+json.userMessage[i].placeName;
							let td7=document.createElement("td");
								td7.innerHTML=json.userMessage[i].email;
					tr.appendChild(td1);
					tr.appendChild(td2);
					tr.appendChild(td3);
					tr.appendChild(td4);
					tr.appendChild(td5);
					tr.appendChild(td6);
					tr.appendChild(td7);
			insertPoint.appendChild(tr);
			
		
					}	
			
		
		}
	});

}
function testString(val,jsonvalues){
	let flag=false;
	let man="男";
	let woman="女";
	if(typeof val != "undefined" || val != null || val != ""){
		if(jsonvalues.id.toString().indexOf(val)!=-1){
			flag=true;
		}
		if(jsonvalues.userName.toString().indexOf(val)!=-1){
			flag=true;
		}
		if(jsonvalues.password.toString().indexOf(val)!=-1){
			flag=true;
		}
		if(jsonvalues.sex.toString().indexOf(val)!=-1){
			flag=true;
		}
		if(jsonvalues.sex==0){
			if(man.indexOf(val)!=-1){
				flag=true;
			}
		}
		if(jsonvalues.sex==1){
			if(woman.indexOf(val)!=-1){
				flag=true;
			}
		}
		
		if(jsonvalues.email.toString().indexOf(val)!=-1){
			flag=true;
		}
		if(jsonvalues.place.toString().indexOf(val)!=-1){
			flag=true;
		}if(jsonvalues.provinceName.indexOf(val)!=-1){
			flag=true;
		}
		if(jsonvalues.placeName.indexOf(val)!=-1){
			flag=true;
		}
		for(var o=0;o<jsonvalues.hobbyName.length;o++){
			if(jsonvalues.hobbyName[o].toString().indexOf(val)!=-1){
				flag=true;
			}
		}
	
		
	}
	else{
		flag=true;
	}
	return flag;

}
function testsear(jsonvalues){
	let flag=false;
	let val=document.getElementById("searchBox").value;
	if(val.indexOf(",")==-1){
		flag=testString(val,jsonvalues);
		
	}else{
		
		let end=val;
		while(end.indexOf(",")!=-1&&!flag){
			val=end.substring(0,end.indexOf(","));
			end=end.substring(end.indexOf(",")+1);
			flag=testString(val,jsonvalues);
			
		}
		if(!flag){
			flag=testString(end,jsonvalues);
		}
	}
	return flag;
}
function firstLoad(pageNumber,endpageNumber){
	document.getElementById("showall").style.visibility="hidden";
	let page=1;
	let au=new AjaxUtils;
	let param="usermessageadmin.servlet?param=get";
	let div=document.getElementById("pageNumberInsert");
	div.innerHTML="";
	au.ajaxRequest(param,(xhr)=>{
		let json=JSON.parse(xhr.responseText);
		// 拿到全部页数
		var Allpage=1;
			if(Math.floor(json.userMessage.length/5)==json.userMessage.length/5)
			{
				Allpage=json.userMessage.length/5;
			}
			else{
				Allpage=Math.floor(json.userMessage.length/5)+1
			}
			
				
		
		
		// 判断是不是跳转最后一页
		if(endpageNumber==0){
			if(pageNumber!=null&&pageNumber!=""){
				page=pageNumber;
				Nowpage=page;
			}
		}else{
			page=Allpage;
			Nowpage=page
		}
				
		// 添加跳转
		for(let pa=0;pa<Allpage;pa++){
						let button=document.createElement("button");
						button.addEventListener("click",()=>{
							firstLoad(pa+1,0);
						},false);
						button.innerHTML=pa+1;
						div.appendChild(button);
				}
		if(Nowpage==Allpage||endpageNumber!=0){
			document.getElementById("pageNext").style.visibility="hidden";
		}
		else{
			document.getElementById("pageNext").style.visibility="visible";
		}
		if(Nowpage==1||pageNumber==1){
			document.getElementById("pageLast").style.visibility="hidden";
		}
		else{
			document.getElementById("pageLast").style.visibility="visible";
		}
		
		let insertPoint=document.getElementById("usermessageInsertPoint");
		insertPoint.innerHTML="<tr style='background:red'><td><input type='checkbox'  id='checkAll' ></td><td>用户名</td><td>密码</td><td>性别</td><td>爱好</td><td>来自于</td><td>邮箱</td></tr>";
		for(let i=0;i<json.userMessage.length;i++){
			if((i+1)<=(page*5)&&(i+1)>=(page*5-4)){
			
							let tr=document.createElement("tr");
							let td1=document.createElement("td");
										let input1=document.createElement("input");
										input1.type="checkbox";
										input1.className="list";
										input1.value=json.userMessage[i].id;
										td1.appendChild(input1);
							let td2=document.createElement("td");
								td2.innerHTML=json.userMessage[i].userName;
							let td3=document.createElement("td");
								td3.innerHTML=json.userMessage[i].password;
							let td4=document.createElement("td");
								if(json.userMessage[i].sex==1){
								td4.innerHTML="女";
								}else{
								td4.innerHTML="男";
								}
							let td5=document.createElement("td");
								for(let m=0;m<json.userMessage[i].hobbyName.length;m++){
									if(m==0){
										td5.innerHTML=json.userMessage[i].hobbyName[m];
									}
									else{td5.innerHTML=td5.innerHTML+","+json.userMessage[i].hobbyName[m];
									}
								}
							let td6=document.createElement("td");
							
								td6.innerHTML=json.userMessage[i].provinceName+","+json.userMessage[i].placeName;
							let td7=document.createElement("td");
								td7.innerHTML=json.userMessage[i].email;
					tr.appendChild(td1);
					tr.appendChild(td2);
					tr.appendChild(td3);
					tr.appendChild(td4);
					tr.appendChild(td5);
					tr.appendChild(td6);
					tr.appendChild(td7);
			insertPoint.appendChild(tr);
			
		
					
			}
		
		}
	});
}
function checke(){
	var allcheck=document.getElementsByClassName("list");
	
	if(document.getElementById("checkAll").checked==true){
	for(var i=0;i<allcheck.length;i++){
		  var obj = allcheck[i];
		  obj.checked=true;
		  }
	}
}

	window.addEventListener("load",init,false);